
package com.hua.controller;



import com.hua.pojo.Advice;
import com.hua.pojo.User;
import com.hua.service.AdviceService;
import com.hua.utils.HandleTokenUtils;
import com.hua.utils.JwtUtils;
import com.hua.utils.Result;
import io.swagger.annotations.Api;

import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hua
 * @since 2023-12-24
 */
@Api(tags = "建议管理")
@RestController
@RequestMapping("/advice")
public class AdviceController {


    @Autowired AdviceService adviceService;
    @Autowired//处理jwt是否有效
    private HandleTokenUtils handleTokenUtils;
    @Autowired//解析jwt
    private JwtUtils jwtUtils;



    @ApiOperation("使用者对饭堂的建议API")
    @PostMapping("/addUserAdvice")
    public Result addUserAdvice(@RequestBody Map<String, Object> requestData){

        String token=(String) requestData.get("token");//token
        int canteenId = (int) requestData.get("canteenId");//建议的饭堂id
        String adviceText = (String) requestData.get("adviceText");//建议内容


        /**
         * 处理token
         */
        //判断jwt是否还有效
        boolean b = handleTokenUtils.checkJWTIsLost(token, User.class);
        if(!b){//登录失效
            return Result.loginFailure();
        }
        //解析token到user对象
        User user = jwtUtils.parseToken(token, User.class);

        if(adviceText.isEmpty()){//建议的内容不能为空
            return Result.fail("建议内容不能为空~");
        }else{
            Advice newAdvice=new Advice();
            newAdvice.setUserId(user.getId());
            newAdvice.setCanteenId(canteenId);
            newAdvice.setAdviceText(adviceText);

            //插入建议到数据库
            int i = adviceService.addUserAdvice(newAdvice);
            if(i==1){
                return Result.fail("发送成功~");
            }else{
                return Result.fail("系统出错，稍后再试~");
            }

        }

    }

    @ApiOperation("返回每个饭堂对应的建议API")
    @PostMapping("/getCanteenAdviceList")
    public Result getCanteenAdviceList(@RequestBody Map<String, Object> requestData){

        String token=(String) requestData.get("token");//token
        int canteenId = (int) requestData.get("canteenId");//建议的饭堂id
        int pageNo = (int) requestData.get("pageNo");//第几页
        int pageSize = (int) requestData.get("pageSize");//每页大小


        /**
         * 处理token
         */
        //判断jwt是否还有效
        boolean b = handleTokenUtils.checkJWTIsLost(token, User.class);
        if(!b){//登录失效
            return Result.loginFailure();
        }
        //解析token到user对象
        User user = jwtUtils.parseToken(token, User.class);

        //判断该用户是不是管理者
        if(user.getPermission()>=2){//是，放行
            Map<String, Object> canteenAdviceList = adviceService.getCanteenAdviceList(canteenId, pageNo, pageSize);
            return Result.success(canteenAdviceList,"获取成功！提醒：两层data");
        }else{
            return Result.fail("权限不足");
        }




    }



}

